<?php
/**
 * Created by leiying
 * Date: 2017/11/15
 * Time: 17:13
 */

/**获取符合条件的商品列表
 * @param $category_id
 * @param $sort_type  DESC / ASC
 * @param $sort_action
 * @param $page
 * @param $page_size
 * @author leiying
 */
function get_goods($category_id,$sort_type,$sort_action,$page,$page_size){
    $where = ' AND is_up=1 ';
    switch($sort_action){
        case 'all'://综合
            $order = 'add_time '.$sort_type;
            break;
        case 'sales'://销量
            $order = 'sale_count '.$sort_type.',add_time desc';
            break;
        case 'price'://价格
            $order = 'shop_price '.$sort_type.',add_time desc';
            break;
        case 'new'://新品
            $order = 'add_time '.$sort_type;
            $where .= ' AND is_new=1 ';
            break;
        case 'comment'://评论数
            $order = 'comment_count '.$sort_type.',add_time desc';
            break;
        default:
            GZ_Api::message('sort_action不合法', 2);
            break;
    }

    $count=$GLOBALS['db']->getOne('SELECT count(*) FROM '. $GLOBALS['db']->table("goods").'WHERE cat_id='.$category_id.$where.' ORDER BY '.$order);
    $page_count = ($count > 0) ? intval(ceil($count / $page_size)) : 1;

    $sql = 'SELECT `goods_id`,`goods_name`,`goods_cover`,`shop_price` FROM '. $GLOBALS['db']->table("goods").'WHERE cat_id='.$category_id.$where.' ORDER BY '.$order;

    $info = $GLOBALS['db']->selectLimit($sql, $page_size, ($page-1) * $page_size);


    $list = array();
    $arr = array();

    while ($row=$GLOBALS['db']->fetchRow($info)) {

        $arr['goods_id']=$row['goods_id'];
        $arr['goods_name']=$row['goods_name'];
        $arr['goods_cover']=GZ_Api::get_url($row['goods_cover']);
        $arr['shop_price']= $row['shop_price']?"￥".$row["shop_price"]:"0";
        $list[] = $arr;
    }


    $pager = array(
        "total"  => $count,
        "count"  => count($list),
        "more"   => $page < $page_count ? 1 : 0
    );

    $res = array('list' =>$list, 'pager' => $pager);

    return $res;
}









